<?php

/**
 * CSS/JS整合打包控制器
 *
 * @package Controller
 * @author  chengxuan <i@chengxuan.li>
 */
class Minify_ShowController extends AbsController {
    
	/**
	 * 不需要登录就可以访问
	 *
	 * @var Boolean
	 */
	protected $_need_login = false;
	
    public function indexAction() {
        
        include APP_PATH . '/library/Thirdpart/Minify/bootstrap.php';
        
        // set config path defaults
        $min_configPaths = array(
            'base'   => CONF_PATH . '/minify-config.php',
            'test'   => CONF_PATH . '/minify-config-test.php',
        );
        
        // check for custom config paths
        if (!empty($min_customConfigPaths) && is_array($min_customConfigPaths)) {
            $min_configPaths = array_merge($min_configPaths, $min_customConfigPaths);
        }
        
        // load config
        require $min_configPaths['base'];
        
        if (isset($_GET['test'])) {
            include $min_configPaths['test'];
        }
        
        // setup factories
        $defaultFactories = array(
            'minify' => function (Minify_CacheInterface $cache) {
                return new Minify($cache);
            },
            'controller' => function (Minify_Env $env, Minify_Source_Factory $sourceFactory) {
                return new Minify_Controller_MinApp($env, $sourceFactory);
            },
        );
        
        if (!isset($min_factories)) {
            $min_factories = array();
        }
        $min_factories = array_merge($defaultFactories, $min_factories);
        
        // use an environment object to encapsulate all input
        $server = $_SERVER;
        if ($min_documentRoot) {
            $server['DOCUMENT_ROOT'] = $min_documentRoot;
        }
        $env = new Minify_Env(array(
            'server' => $server,
        ));
        
        // TODO probably should do this elsewhere...
        $min_serveOptions['minifierOptions']['text/css']['docRoot'] = $env->getDocRoot();
        $min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
        
        // auto-add targets to allowDirs
        foreach ($min_symlinks as $uri => $target) {
            $min_serveOptions['minApp']['allowDirs'][] = $target;
        }
        
        if ($min_allowDebugFlag) {
            // TODO get rid of static stuff
            $min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
        }
        
        if (!empty($min_concatOnly)) {
            $min_serveOptions['concatOnly'] = true;
        }
        
        if ($min_errorLogger) {
            if (true === $min_errorLogger) {
                $min_errorLogger = FirePHP::getInstance(true);
            }
            // TODO get rid of global state
            Minify_Logger::setLogger($min_errorLogger);
        }
        
        // check for URI versioning
        if (null !== $env->get('v') || preg_match('/&\\d/', $env->server('QUERY_STRING'))) {
            $min_serveOptions['maxAge'] = 31536000;
        }
        
        // need groups config?
        if (null !== $env->get('g')) {
            // we need groups config
            $min_serveOptions['minApp']['groups'] = \Model\Minify::loadGroupConfig();
        }
        
        // cache defaults
        if (!isset($min_cachePath)) {
            $min_cachePath = '';
        }
        if (!isset($min_cacheFileLocking)) {
            $min_cacheFileLocking = true;
        }
        if (is_string($min_cachePath)) {
            $cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
        } else {
            // assume it meets interface.
            $cache = $min_cachePath;
        }
        /* @var Minify_CacheInterface $cache */
        
        $minify = call_user_func($min_factories['minify'], $cache);
        /* @var Minify $minify */
        
        if (!$env->get('f') && $env->get('g') === null) {
            // no spec given
            $msg = '<p>No "f" or "g" parameters were detected.</p>';
            $url = 'https://github.com/mrclay/minify/blob/master/docs/CommonProblems.wiki.md#long-url-parameters-are-ignored';
            $defaults = $minify->getDefaultOptions();
            $minify->errorExit($defaults['badRequestHeader'], $url, $msg);
        }
        
        $sourceFactoryOptions = array();
        
        // translate legacy setting to option for source factory
        if (isset($min_serveOptions['minApp']['noMinPattern'])) {
            $sourceFactoryOptions['noMinPattern'] = $min_serveOptions['minApp']['noMinPattern'];
        }
        $sourceFactory = new Minify_Source_Factory($env, $sourceFactoryOptions, $cache);
        
        $controller = call_user_func($min_factories['controller'], $env, $sourceFactory);
        /* @var Minify_ControllerInterface $controller */
        
        $minify->serve($controller, $min_serveOptions);
        
        return false;
    }
}
